package web.common.internal.core.security;

import web.common.core.security.PasswordEncoder;
import web.common.core.security.PasswordEncoderType;

/**
 * @author Jin Zheng
 * @since 1.0 2022-01-20
 */
public class NoopPasswordEncoder implements PasswordEncoder {
	private static final String ENCODED_TPL = "{%s}%s";
	private static final PasswordEncoderType TYPE = PasswordEncoderType.NOOP;

	public NoopPasswordEncoder() {
	}

	@Override
    public String encode(String origin) {
        return String.format(ENCODED_TPL, TYPE, origin);
    }

    @Override
    public boolean matches(String origin, String encoded) {
		var info = PasswordEncoderFacade.parseInfo(encoded);
		if (info.getType() != TYPE) {
			throw new UnsupportedOperationException("错误类型: " + info.getType());
		}
        return info.getEncoded().equals(origin);
    }
}
